<template>
	<view>
		<xiaolu-tree @itemClick="handleClick" :checkList="checkList" v-if="tree.length>0" :props="prop"
			@sendValue="confirm" :isCheck="true" :trees="tree" />
		<view v-else>
			<van-empty description="暂无人员"></van-empty>
		</view>
		<view class="btn box_sizing">
			<button class="sureBtn" type="primary" @click="backConfirm">确认</button>
		</view>
	</view>
</template>

<script>
	import XiaoluTree from '@/components/xiaolu-tree/tree.vue';
	import dataList from './data.js'
	// import dataList from './test.js'
	export default {
		components: {
			XiaoluTree
		},
		data() {
			return {
				unitTree: {},
				list: [],
				params: {
					单位id: null
				},
				params1: {
					orgId: this.$config.groupId,
					pagination: {
						current: 1,
						pageSize: 20
					},
					sorter: {
						field: "id",
						order: 0
					}
				},
				tree: [],
				checkList: [],
				prop: {},
				aprop: {
					label: 'name',
					children: 'children',
					multiple: true
				},
				bprop: {
					label: 'name',
					children: 'children',
					multiple: true,
					checkStrictly: true
				},
				cprop: {
					label: 'name',
					children: 'children',
					multiple: false,
					nodes: false
				},
				dprop: {
					label: 'name',
					children: 'children',
					multiple: false,
					nodes: true
				}
			}
		},
		onLoad(o) {
			if (o.type == 0) {
				this.prop = this.aprop;
				this.checkList = JSON.parse(o.item)
				this.checks()
			} else if (o.type == 1) {
				this.prop = this.bprop;
				this.checkList = JSON.parse(o.item)
				// this.checks()
			} else if (o.type == 2) {
				this.prop = this.cprop;
			} else {
				this.prop = this.dprop;
				this.checkList = JSON.parse(o.item)
				this.params1.projectIds = [+o.projId]
				this.params1.positionId = +o.roleId
			}
			this.getWorkerList()
		},
		onReady() {
		},
		methods: {
			getWorkerList() {
				uni.showLoading({
					title:"加载中..."
				})
				this.$http2.post('/sys/user/list', {
					orgId: this.$config.groupId,
					pagination: {
						current: 1,
						pageSize: 10
					},
					sorter: {
						field: "id",
						order: 0
					}
				}).then(res => {
					if (res.code === 10000) {
						this.total = res.data.total
						const httpArr = []
						for(let i = 0; i < Math.ceil(this.total / 20); i++) {
							httpArr.push(new Promise((resolve, reject) => {
								let params = JSON.parse(JSON.stringify(this.params1))
								params.pagination.current = i + 1
								this.$http2.post('/sys/user/list', params).then(res => {
									if (res.code === 10000) {
										res.data.list = res.data.list || []
										res.data.list.forEach(item => {
											item.checked = false
											item.user = true
											item.children = []
											item.pid = ''
										})
										resolve(res.data.list)
									}
								})
							}))
							
						}
						Promise.all(httpArr).then(res => {
							this.tree = res.reduce((a, b) => a.concat(b))
							this.checks()
						})
					}
				})
			},
			//检查默认选中,要是字段钟本身包含了checked字段就不需要这个
			checks() {
				var that = this;
				for (var i = 0, len = that.tree.length; i < len; i++) {
					for (var j = 0, lens = that.checkList.length; j < lens; j++) {
						if (that.checkList[j].id === that.tree[i].id) {
							that.$set(that.tree[i], 'checked', true)
							break
						} else {
							that.$set(that.tree[i], 'checked', false)
						}
					}
				}
			},
			//获取选中的
			confirm(val) {
				console.log(val)
				this.checkList = val
			},
			backConfirm() {
				console.log(this.type)
				console.log(this.checkList)
				var pages = getCurrentPages();
				var currPage = pages[pages.length - 1]; //当前页面
				var prevPage = pages[pages.length - 2]; //上一个页面
				//prevPage.query = this.checkList//小程序写法
				// prevPage.$vm.query = this.checkList//h5写法
				prevPage.$vm.query1 = this.checkList
				uni.setStorageSync('query1', this.checkList)
				// prevPage.$vm.params.执行人id = this.checkList[0].id
				uni.navigateBack();
			}
		}
	}
</script>

<style lang="scss">
	.box_sizing {
		-webkit-box-sizing: border-box;
		-moz-box-sizing: border-box;
		box-sizing: border-box;
	}

	.btn {
		position: fixed;
		bottom: 0;
		padding: 10px;
		background-color: #fff;
		width: 100%;

		.sureBtn {
			background-color: #1152FC;
			color: #fff;
		}
	}
</style>
